/*
 * Copyright (c) 2016-2021, Hangzhou Qulian Technology Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "keccak256.h"
#include "ql_type.h"
#include "CuTest.h"

// test function
void keccak256_test(CuTest* tc){
    log_debug("----keccak256 test----");
    // res: bbd99bc84b4133463be1a715e57107ffbccbaf93df5d594c95dbe4a59ad3852e
    OhByteType *in  = (OhByteType *) "from=0xfa4a201392ecfb8636efc0028554df66218a1b0f&to=0xfa4a201392ecfb8636efc0028554df66218a1b0f&value=0x0&payload=0x0&timesTamp=0x1660cb9be3bfba01&nonce=0x3413&opcode=0&extra=&vmtype=EVM&version=2.5&extraid=&cname=";
    log_debug("[input  data] %s",in);
    OhByteType *out = (OhByteType *) malloc(sizeof(OhByteType) * 32);
    if (out == NULL)
        log_error("malloc space err");
    keccak256(in, strlen((char*)in), &out);

    OhByteType buf[65];
    OhUint8Type i;
    for(i = 0; i < 32; i ++){
        buf[i*2]   = ((out[i] & 0xf0) >> 4) <=9 ?  ((out[i] & 0xf0) >> 4) + '0' : ((out[i] & 0xf0) >> 4) + 'a' - 10;
        buf[i*2+1] = ((out[i] & 0x0f)     ) <=9 ?  ((out[i] & 0x0f)     ) + '0' : ((out[i] & 0x0f)     ) + 'a' - 10;
    }
    buf[64] = '\0';
    CuAssertStrEquals(tc, "bbd99bc84b4133463be1a715e57107ffbccbaf93df5d594c95dbe4a59ad3852e",(char*)buf);

    free(out);
}

CuSuite* keccak256_test_suite(void)
{
    CuSuite* suite = CuSuiteNew();
    SUITE_ADD_TEST(suite, keccak256_test);
    return suite;
}
